
#ifndef _CAN_CLIENT_H_
#define _CAN_CLIENT_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include "linux/can.h"
#include "linux/can/raw.h"
// #include "lib.h"
#include "stdint.h"
#include <vector>
#include <iostream>



namespace canbus
{

    const int32_t CAN_FRAME_SIZE = 8;
    const int32_t MAX_CAN_SEND_FRAME_LEN = 1;
    const int32_t MAX_CAN_RECV_FRAME_LEN = 10; // 每次收两条处理
    const int32_t CANBUS_MESSAGE_LENGTH = 8;  // according to ISO-11891-1
    enum ErrorCode
    {
        OK = 0,
        CANBUS_ERROR = 2000,
        CAN_CLIENT_ERROR_BASE = 2100,
        CAN_CLIENT_ERROR_OPEN_DEVICE_FAILED = 2101,
        CAN_CLIENT_ERROR_FRAME_NUM = 2102,
        CAN_CLIENT_ERROR_SEND_FAILED = 2103,
        CAN_CLIENT_ERROR_RECV_FAILED = 2104
    };

    struct CanFrame
    {
        uint32_t id;
        uint8_t len;
        uint8_t data[8];
        struct timeval timestamp;
        CanFrame() : id(0), len(0), timestamp{0}
        {
            memset(data, 0, sizeof(data));
        }
    };

    // class CanClient
    // {
    // public:
    //     CanClient() = default;
    //     virtual ~CanClient() = default;
    //     virtual bool Init(std::string port) = 0;
    //     virtual ErrorCode Start() = 0;
    //     virtual void Stop() = 0;
    //     virtual ErrorCode Send(const std::vector<CanFrame> &frames, int32_t *const frame_num) = 0;
    //     virtual ErrorCode SendSingleFrame(const std::vector<CanFrame> &frames)
    //     {
    //         int32_t n = 1;
    //         return Send(frames, &n);
    //     }
    //     virtual ErrorCode Receive(std::vector<CanFrame> *const frames, int32_t *const frame_num) = 0;
    //     virtual std::string GetErrorString(const int32_t status) = 0;

    // protected:
    //     bool is_started_ = false;
    // };

}

#endif
